package com.wing.bill.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.dao.BillOrdInvoiceInformationDao;
import com.wing.bill.model.entity.BillOrdInformation;
import com.wing.bill.model.entity.BillOrdInvoiceInformation;
import com.wing.bill.model.request.BillOrdInvoiceInformationForm;
import com.wing.bill.model.response.BillOrdInvoiceInformationVO;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.EnterpricceIdService;
import com.wing.common.utils.JsonResult;
import com.wing.util.PulsarCommons;
import com.wing.util.PulsarUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 账单付款发票信息-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class BillOrdInvoiceInformationServiceImpl extends ServiceImpl<BillOrdInvoiceInformationDao, BillOrdInvoiceInformation> implements BillOrdInvoiceInformationService {

    @Value("${pulsar.client.topicUrl}")
    private String topicUrl;

    @Autowired
    private  BillOrdInformationService billOrdInformationService;
    @Autowired
    private EnterpricceIdService enterpricceIdService;
    @Override
    public int add(BillOrdInvoiceInformationForm billOrdInvoiceInformationForm) {

        //校验

        BillOrdInvoiceInformation billOrdInvoiceInformation =new BillOrdInvoiceInformation();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(BillOrdInvoiceInformation.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(billOrdInvoiceInformationForm, billOrdInvoiceInformation, copyOptions);

        return baseMapper.insert(billOrdInvoiceInformation);
    }

    @Override
    public List<BillOrdInvoiceInformationVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<BillOrdInvoiceInformationVO> pageList4app(Page page, Map<String, Object> params) {
        List<BillOrdInvoiceInformationVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public BillOrdInvoiceInformationVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult addAll(BillOrdInvoiceInformationForm billOrdInvoiceInformationForm) {
        //企业只在回收时开发票
        if(baseMapper.find(billOrdInvoiceInformationForm.getBillNo())==0){//添加第一张修改
            BillOrdInformation billOrdInformation = new BillOrdInformation();
            billOrdInformation.setBillNo(billOrdInvoiceInformationForm.getBillNo());
            billOrdInformation.setEnterpriseId(billOrdInvoiceInformationForm.getEnterpriseId());
            billOrdInformation.setInvoiceStatus(BillOrdInformation.InvoiceStatus.invoiced);
            billOrdInformation.setId((Long) billOrdInformationService.find(billOrdInvoiceInformationForm.getBillNo()).get("id"));
            QueryWrapper<BillOrdInformation> queryWrapper = new QueryWrapper<>();
            billOrdInformationService.update(billOrdInformation,queryWrapper);
            // todo 同步
            try{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", "Update");
                jsonObject.put("data", billOrdInformation);
                PulsarUtils.send(topicUrl+ PulsarCommons.BILL_ORD_INFORMATION_TOPIC,jsonObject.toJSONString());
            }catch (Exception e) {
                log.error(PulsarCommons.BILL_ORD_INFORMATION_TOPIC+"同步失败",e);
            }
        }
        BillOrdInvoiceInformation billOrdInvoiceInformation =new BillOrdInvoiceInformation();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(BillOrdInvoiceInformation.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(billOrdInvoiceInformationForm, billOrdInvoiceInformation, copyOptions);
        if (baseMapper.insert(billOrdInvoiceInformation) > 0) {
            // todo 同步
            try {
                EnterpricceIdVO enterpricceIdVO = enterpricceIdService.getEnterpricceIdVO();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", "add");
                jsonObject.put("data", billOrdInvoiceInformation);
                jsonObject.put("enterpriseId",enterpricceIdVO.getEnterpriseId());
                PulsarUtils.send(topicUrl+ PulsarCommons.BILL_ORD_INVOICE_INFORMATION_TOPIC,jsonObject.toJSONString());
            } catch (PulsarClientException e) {
                log.error(PulsarCommons.BILL_ORD_INVOICE_INFORMATION_TOPIC+"同步失败",e);
            }
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }
}
